<?php

/**
 * @Author: chenjianghui
 * @Date:   2018-07-04 10:29:31
 * @Last Modified by:   chenjianghui
 * @Last Modified time: 2018-08-08 09:50:03
 */
namespace app\common\third;

class QqModel
{
    const server_url = 'https://graph.qq.com';

    /**
     * 获取用户信息
     * @Author   chenjianghui
     * @DateTime 2018-07-04T10:30:27+0800
     * @param    [type]                   $access_token token认证
     * @param    [type]                   $openid       openid
     * @return   [type]                                 [description]
     */
    public static function getUserInfo($access_token, $openid, $appid)
    {
        $params = [
            'access_token'       => $access_token,
            'openid'             => $openid,
            'oauth_consumer_key' => $appid,
        ];
        $paramsStr = self::pararmsToStr($params);

        $route  = '/user/get_user_info?';
        $url    = self::server_url . $route . $paramsStr;
        $result = curl($url);
        $result = json_decode($result, true);
        if ($result['errcode']) {
            renderJson($result['errmsg'], $result['errcode']);
        }
        return $result;
    }

    /*
     * 将请求参数数组转换字符串
     * */
    public static function pararmsToStr($pararms)
    {
        $result = '';
        foreach ($pararms as $key => $vo) {
            $result .= "$key=" . $vo . "&";
        }
        $result = substr($result, 0, -1);
        return $result;
    }
}
